package com.wuyan.wurpc.loadbalancer;

import com.wuyan.wurpc.model.ServiceMetaInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 一致性哈希负载均衡器
 */
@Slf4j
public class ConsistentHashLoadBalancer implements LoadBalancer{

    /**
     * 一致性Hash环，存在虚拟机节点
     */
    private final TreeMap<Integer, ServiceMetaInfo> virtualNodes = new TreeMap<>();

    /**
     * 虚拟节点数
     */
    private static final int VIRTUAL_NODE_NUM = 100;

    @Override
    public ServiceMetaInfo select(Map<String, Object> requestParams, List<ServiceMetaInfo> serviceMetaInfoList) {
        if(serviceMetaInfoList.isEmpty()){
            return null;
        }
        //构建虚拟机节点环
        log.info("使用一致性哈希负载均衡器,serviceMetaInfoList ：{}",serviceMetaInfoList);
        for(ServiceMetaInfo serviceMetaInfo : serviceMetaInfoList){
            for(int i = 0; i < VIRTUAL_NODE_NUM; i++){
                int hash = getHash(serviceMetaInfo.getServiceAddress() + '#' + i);
                virtualNodes.put(hash, serviceMetaInfo);
            }
        }
        //获取调用请求的hash值
        int hash = getHash(requestParams);
        //选择最接近且大于等于调用请求的虚拟节点
        Map.Entry<Integer, ServiceMetaInfo> entry = virtualNodes.ceilingEntry(hash);
        if(entry == null){
            //如果没有大于等于调用请求hash值的虚拟节点，则返回首部节点
            entry = virtualNodes.firstEntry();
        }
        return entry.getValue();
    }

    /**
     * Hash算法，可自行实现
     */
    private int getHash(Object key){
        return key.hashCode();
    }

}
